package com.newcapec.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newcapec.common.annotation.SysLog;
import com.newcapec.common.utils.ConfigConstant;
import com.newcapec.common.utils.Constant;
import com.newcapec.common.utils.PageUtils;
import com.newcapec.common.utils.R;
import com.newcapec.common.validator.Assert;
import com.newcapec.common.validator.ValidatorUtils;
import com.newcapec.common.validator.group.AddGroup;
import com.newcapec.common.validator.group.UpdateGroup;
import com.newcapec.modules.health.entity.CustomerEntity;
import com.newcapec.modules.health.entity.StudentEntity;
import com.newcapec.modules.health.service.CustomerService;
import com.newcapec.modules.health.service.StudentService;
import com.newcapec.modules.sys.entity.SysUserEntity;
import com.newcapec.modules.sys.form.PasswordForm;
import com.newcapec.modules.sys.service.SysConfigService;
import com.newcapec.modules.sys.service.SysUserService;
import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * 系统用户
 *
 * @author steven
 */
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private SysConfigService sysConfigService;

	/**
	 * 所有用户列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("sys:user:list")
	public R list(@RequestParam Map<String, Object> params){
		params.put("type", 2);
		PageUtils page = sysUserService.queryPage(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@GetMapping("/info")
	public R info(){
		SysUserEntity sysUser = getUser();
		StudentEntity student = studentService.getOne(
				new QueryWrapper<StudentEntity>().eq("user_id", sysUser.getUserId())
		);
		CustomerEntity customer = customerService.get(student.getCustomerId());
		return R.ok().put("user", getUser()).put("student", student).put("customer", customer);
	}
	
	/**
	 * 修改登录用户密码
	 */
	@SysLog("修改密码")
	@PostMapping("/password")
	public R password(@RequestBody PasswordForm form){
		Assert.isBlank(form.getNewPassword(), "新密码不为能空");
		
		//sha256加密
		String password = new Sha256Hash(form.getPassword(), getUser().getSalt()).toHex();
		//sha256加密
		String newPassword = new Sha256Hash(form.getNewPassword(), getUser().getSalt()).toHex();
				
		//更新密码
		boolean flag = sysUserService.updatePassword(getUserId(), password, newPassword);
		if(!flag){
			return R.error("原密码不正确");
		}
		
		return R.ok();
	}
	
	/**
	 * 用户信息
	 */
	@GetMapping("/info/{userId}")
	@RequiresPermissions("sys:user:info")
	public R info(@PathVariable("userId") Long userId){
		SysUserEntity user = sysUserService.getById(userId);

		return R.ok().put("user", user);
	}
	
	/**
	 * 保存用户
	 */
	@SysLog("保存用户")
	@PostMapping("/save")
	@RequiresPermissions("sys:user:save")
	public R save(@RequestBody SysUserEntity user){
		ValidatorUtils.validateEntity(user, AddGroup.class);
		Long roleId = Long.parseLong(sysConfigService.getValue(ConfigConstant.STUDENT_ROLE_ID));
		
		user.setCreateUserId(getUserId());
		user.setType(Constant.UserType.USER.getValue());
		user.setRoleIdList(Arrays.asList(roleId));
		sysUserService.saveUser(user);
		
		return R.ok();
	}
	
	/**
	 * 修改用户
	 */
	@SysLog("修改用户")
	@PutMapping("/update")
	@RequiresPermissions("sys:user:update")
	public R update(@RequestBody SysUserEntity user){
		ValidatorUtils.validateEntity(user, UpdateGroup.class);
		Long roleId = Long.parseLong(sysConfigService.getValue(ConfigConstant.STUDENT_ROLE_ID));

		user.setCreateUserId(getUserId());
		user.setType(Constant.UserType.USER.getValue());
		user.setRoleIdList(Arrays.asList(roleId));
		sysUserService.update(user);
		
		return R.ok();
	}
	
	/**
	 * 删除用户
	 */
	@SysLog("删除用户")
	@DeleteMapping("/delete")
	@RequiresPermissions("sys:user:delete")
	public R delete(Long[] ids){
		if(ArrayUtils.contains(ids, 1L)){
			return R.error("系统管理员不能删除");
		}

		if(ArrayUtils.contains(ids, getUserId())){
			return R.error("当前用户不能删除");
		}
		
		sysUserService.deleteBatch(ids);
		
		return R.ok();
	}
}
